"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.User_Spend_DailyDao = void 0;
const Config_1 = require("../../src/config/Config");
const DbConfig_1 = require("../../src/config/DbConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
class User_Spend_DailyDao extends DaoBase_1.DaoBase {
    static add_user_spend_daily(param) {
        return __awaiter(this, void 0, void 0, function* () {
            let userid = param["userid"];
            let type = param["type"];
            let cost = param["cost"];
            let dateStr = DateUtil_1.default.formatDateStr3(DateUtil_1.default.now());
            let sql = `select * from t_user_spend_daily where userid= ? and date= ?`;
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sqlParams = [userid, dateStr];
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows.length) {
                sql = `update t_user_spend_daily set ${type} = ${type} + ${cost} where userid= ${userid} and date= '${dateStr}'`;
                rows = yield client.queryPromise(sql, sqlParams);
            }
            else {
                sql = `insert into t_user_spend_daily(userid,${type},date) values(${userid},${cost},'${dateStr}')`;
                rows = yield client.queryPromise(sql, sqlParams);
            }
        });
    }
    static clear() {
        return __awaiter(this, void 0, void 0, function* () {
            let now = DateUtil_1.default.now();
            let delete_time = now - Config_1.Config.OneDay * 100; //保留100天记录
            // let delete_time: number = now - Config.OneDay * 3; //保留100天记录
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "delete from t_user_spend_daily where date < ?";
            let sqlParams = [DateUtil_1.default.formatDateStr3(delete_time)];
            yield client.queryPromise(sql, sqlParams);
        });
    }
    static get_user_spend_daily(kind, startTime, endTime) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = `select  t_user_spend_daily.userid,  t_user_spend_daily.${kind},  t_user_spend_daily.date, headimg, nick_name  from t_user_spend_daily  left join t_users on t_user_spend_daily.userid=t_users.userid where t_user_spend_daily.date >= ? and t_user_spend_daily.date < ? `;
            let sqlParams = [startTime, endTime];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_user_spend_daily===", sql, sqlParams);
            return results;
        });
    }
    static get_user_spend_by_date(startTime, endTime) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = `select * from t_user_spend_daily where date >= ? and date < ?`;
            let sqlParams = [startTime, endTime];
            let results = yield client.queryPromise(sql, sqlParams);
            return results;
        });
    }
}
exports.User_Spend_DailyDao = User_Spend_DailyDao;
User_Spend_DailyDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
